package cn.tedu._05weibo.controller;


import cn.tedu._05weibo.base.response.JsonResult;
import cn.tedu._05weibo.base.response.StatusCode;
import cn.tedu._05weibo.mapper.WeiboMapper;
import cn.tedu._05weibo.pojo.dto.WeiboDto;
import cn.tedu._05weibo.pojo.vo.UserVO;
import cn.tedu._05weibo.pojo.vo.WeiboDetailsVO;
import cn.tedu._05weibo.pojo.vo.WeiboVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RestController
@RequestMapping("v1/weibo/")
@Api(tags = "02.微博模块")
@Validated
public class WeiboController {
    @Autowired(required = false)
    private WeiboMapper weiboMapper;




    //发布微博
    @ApiOperation(value = "发布微博功能")
    @PostMapping("insert")
    public JsonResult insertWeibo(@RequestBody WeiboDto weiboDto ,@ApiIgnore HttpSession session){
        UserVO userVO=(UserVO) session.getAttribute("user");
        weiboDto.setUserId(userVO.getId());
        weiboMapper.insertWeibo(weiboDto);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }



    //微博首页
    @ApiOperation(value = "获取微博列表功能")
    @GetMapping("selectIndex")
    public JsonResult selectIndexWeibo(){
        return  new JsonResult(StatusCode.OPERATION_SUCCESS,weiboMapper.selectIndexWeibo());
    }

    //微博细节页面
    @ApiOperation(value = "获取微博详情功能")
    @GetMapping("selectById")
    @ApiImplicitParam(name = "id",value = "微博ID",required = true,dataType = "long")

    public JsonResult selectByWeiboId(@Range(min = 200,max = 300,message = "微博编号必须在200到300之间") @RequestParam Long id){
        if (id<0){
            throw new IllegalArgumentException("微博编号不能小于0");
        }
        return new JsonResult(StatusCode.OPERATION_SUCCESS,weiboMapper.selectByWeiboId(id));
    }



}
